package com.hsource.hsourcedemo.controller;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.hsource.common.utils.CommonAuth;
import com.hsource.common.utils.CommonResp;
import com.hsource.common.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @Author itoug
 * @Crreate 2020-09-03 16:30
 */
@RestController
@RequestMapping("/hsource-demo/user")
@Api(tags = "demo-用户")
@RefreshScope
public class UserController {

    public static final String YZ_LOGIN_URL="https://account.youzan.com/api/login/native/open/login-or-register.json";

    public static final String CLIENT_ID="3406c7f6b77c60feb8";

    public static final String OPEN_USER_ID="90945959";

    @Value(value ="${YZ_LOGIN_URL_1:000000}")
    private String YZ_LOGIN_URL_1;

    @PostMapping(value = "/login")
    @ResponseBody
    @ApiOperation(value="模拟登录", notes="模拟登录")
    public Object login(@RequestBody LoginVO vo)
    {
        System.out.println("phone="+vo.getPhone()
                +",smscode="+vo.getSmscocde()
                +",devicebrand="+vo.getDevicebrand()
                        +",devicemodel="+vo.getDevicemodel()
                        +",devicenumber="+vo.getDevicenumber()
                        +",flag="+vo.getFlag()
                        +",geo_latitude="+vo.getGeo_latitude()
                        +",geo_longitude="+vo.getGeo_longitude());

        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("client_id", CLIENT_ID);
        paramMap.put("open_user_id",OPEN_USER_ID);

        String result= HttpUtil.post(YZ_LOGIN_URL, paramMap);

        if (StringUtils.isEmpty(result)){
            return new CommonResp().failed();
        }else{
            JSONObject jsonObject = JSONUtil.parseObj(result);
            JSONObject jsonData = jsonObject.getJSONObject("data");

            CommonAuth commonAuth = new CommonAuth();
            CommonResp commonResp = new CommonResp();
            if(jsonData!=null) {
                commonAuth.setCookie_key(jsonData.get("cookie_key").toString());
                commonAuth.setCookie_value(jsonData.get("cookie_value").toString());
                commonAuth.setToken("Jwt12yvskcelstl");
            }
            commonResp.setAuth(commonAuth);
            return commonResp.success();
        }

    }


    @GetMapping(value = "/nacos")
    @ResponseBody
    @ApiOperation(value = "Nacos动态配置")
    public String testNacosConfig(){
        return YZ_LOGIN_URL_1;
    }
}
